package com.ruoyi.bookkeeping.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.query.MPJLambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.bookkeeping.domain.BookDynamic;
import com.ruoyi.bookkeeping.domain.BookFriends;
import com.ruoyi.bookkeeping.domain.BookSort;
import com.ruoyi.bookkeeping.domain.param.AddBookDynamicReplyParam;
import com.ruoyi.bookkeeping.domain.vo.BookDynamicReplyVo;
import com.ruoyi.bookkeeping.mapper.BookFriendsMapper;
import com.ruoyi.bookkeeping.mapper.BookSortMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import com.ruoyi.bookkeeping.mapper.BookDynamicReplyMapper;
import com.ruoyi.bookkeeping.domain.BookDynamicReply;
import com.ruoyi.bookkeeping.service.IBookDynamicReplyService;

/**
 * 动态回复Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-24
 */
@Service
public class BookDynamicReplyServiceImpl extends ServiceImpl<BookDynamicReplyMapper, BookDynamicReply> implements IBookDynamicReplyService
{
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private BookFriendsMapper bookFriendsMapper;

    @Override
    public List<BookDynamicReplyVo> selectBookDynamicReplyVoList(Long dynamicId,Long userId) {
        //查动态下的所有回复
        List<BookDynamicReplyVo> dynamicReplyVos = baseMapper.selectBookDynamicReplyVoList(dynamicId);
        for (BookDynamicReplyVo b:dynamicReplyVos) {
            if(ObjectUtil.equal(b.getReplyUserId(),userId)){
                b.setIsMyReply(1);
            }else {
                b.setIsMyReply(0);
            }
            if(!Objects.equals(b.getReplyUserId(), b.getBeReplyUserId())){
                LambdaQueryWrapper<BookFriends> bookFriends = new LambdaQueryWrapper<>();
                bookFriends.eq(BookFriends::getUserId,b.getReplyUserId())
                        .eq(BookFriends::getFriendsId,b.getBeReplyUserId());
                BookFriends friends = bookFriendsMapper.selectOne(bookFriends);
                b.setFriendsRemark(friends.getFriendsRemark());
                LambdaQueryWrapper<BookFriends> beBookFriends = new LambdaQueryWrapper<>();
                beBookFriends.eq(BookFriends::getUserId,b.getBeReplyUserId())
                        .eq(BookFriends::getFriendsId,b.getReplyUserId());
                BookFriends beFriends = bookFriendsMapper.selectOne(beBookFriends);
                b.setFriendsRemark(beFriends.getFriendsRemark());
            }
        }
        return dynamicReplyVos;
    }

    /**
     * 新增动态回复
     * @param addBookDynamicReplyParam
     * @param userId
     * @return
     */
    @Override
    public int insertBookDynamicReply(AddBookDynamicReplyParam addBookDynamicReplyParam, Long userId)
    {
        BookDynamicReply bookDynamicReply = new BookDynamicReply();
        BeanUtils.copyProperties(addBookDynamicReplyParam,bookDynamicReply);
        bookDynamicReply.setCreateTime(DateUtils.getNowDate());
        bookDynamicReply.setReplyUserId(userId);
        SysUser sysUser = sysUserMapper.selectUserById(bookDynamicReply.getReplyUserId());
        if(ObjectUtil.isEmpty(sysUser)){
            return 0;
        }
        bookDynamicReply.setCreateBy(sysUser.getNickName());
        return baseMapper.insert(bookDynamicReply);
    }


    /**
     * 删除动态回复信息
     * 
     * @param dynamicReplyId 动态回复主键
     * @return 结果
     */
    @Override
    public int deleteBookDynamicReplyByDynamicReplyId(Long dynamicReplyId,Long userId)
    {
        LambdaQueryWrapper<BookDynamicReply> bookDynamicReplyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bookDynamicReplyLambdaQueryWrapper.eq(BookDynamicReply::getReplyUserId,userId)
                .eq(BookDynamicReply::getDynamicReplyId,dynamicReplyId);
        BookDynamicReply bookDynamicReply = baseMapper.selectOne(bookDynamicReplyLambdaQueryWrapper);
        if(ObjectUtil.isEmpty(bookDynamicReply)){
            return 0;
        }
        return baseMapper.deleteById(dynamicReplyId);
    }

    @Override
    public int goodBookDynamic(Long dynamicReplyId, Long userId, Long goodOrCancle) {
        LambdaQueryWrapper<BookDynamicReply> bookDynamicReplyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bookDynamicReplyLambdaQueryWrapper.eq(BookDynamicReply::getDynamicReplyId,dynamicReplyId);
        BookDynamicReply bookDynamicReply = baseMapper.selectOne(bookDynamicReplyLambdaQueryWrapper);
        if(0 == goodOrCancle){
            bookDynamicReply.setDynamicGoodNum(bookDynamicReply.getDynamicGoodNum() + 1);
        }else {
            bookDynamicReply.setDynamicGoodNum(bookDynamicReply.getDynamicGoodNum() - 1);
        }
        return baseMapper.updateById(bookDynamicReply);
    }
}
